package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.entity.OrderMessage;
import com.example.entity.ShoppingCar;
import com.example.mapper.ShoppingCarMapper;
import com.example.service.ShoppingCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


@Service
public class ShoppingCarServiceImpl extends ServiceImpl<ShoppingCarMapper, ShoppingCar> implements ShoppingCarService {
    @Autowired
    private ShoppingCarMapper shoppingCarMapper;

    @Autowired
    private ShoppingCarService shoppingCarService;

    public List<ShoppingCar> selectList(ShoppingCar shoppingCar) {
        List<ShoppingCar> list = shoppingCarMapper.selectList(new QueryWrapper<ShoppingCar>().eq("openid", shoppingCar.getOpenid()));
        return list;
    }

    /**
     * 删除购物车指定id
     */
    @Override
    @Transactional
    public Boolean deleteShoppingAll(List<OrderMessage> orderMessage) {
        List<Long> CommodityId = orderMessage.stream()
                .map(OrderMessage::getGoodsId)
                .collect(Collectors.toList());
        LambdaQueryWrapper<ShoppingCar> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCar::getOpenid, orderMessage.get(0).getOpenid())
                .in(ShoppingCar::getCommodityId, CommodityId);
        List<ShoppingCar> shoppingCarList = shoppingCarMapper.selectList(wrapper);
        // 如果购物车不存在那就代表 不是购物车下单
        if (!shoppingCarList.isEmpty()) {
            return shoppingCarService.remove(wrapper);
        }
        return true;
    }
}

